package org.erp.spu.controller;

import org.erp.common.domain.Result;
import org.erp.common.page.DataPageResp;
import org.erp.common.page.PageQueryModel;
import org.erp.spu.entity.bo.SpuModelBo;
import org.erp.spu.entity.dto.SpuModelDto;
import org.erp.spu.entity.vo.SpuModelVo;
import org.erp.spu.service.SpuModelService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static org.erp.common.domain.Result.success;

/**
 * 型号表(SpuModel)表控制层
 *
 * @author dingding
 * @since 2025-11-05 17:05:01
 */
@RestController
@RequestMapping("model")
public class SpuModelController {

    @Resource
    private SpuModelService spuModelService;

    /**
     * 分页查询所有数据
     *
     * @param spuModelBo 查询实体
     * @return 所有数据
     */
    @GetMapping("getListByPage")
    public Result<DataPageResp<SpuModelVo>> selectAll(SpuModelBo spuModelBo) {
        SpuModelDto spuModelDto = new SpuModelDto();
        BeanUtils.copyProperties(spuModelBo, spuModelDto);
        PageQueryModel<SpuModelVo> queryModel = spuModelService.getListByPage(spuModelDto);
        return success(DataPageResp.of(queryModel.getTotal(), queryModel.getDataList()));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result<SpuModelVo> selectOne(@PathVariable Long id) {
        return success(spuModelService.getSpuModelVoById(id));
    }

    /**
     * 获取所有数据
     *
     * @param spuModelBo 筛选条件
     * @return 所有数据
     */
    @GetMapping("getList")
    public Result<List<SpuModelVo>> getList(SpuModelBo spuModelBo) {
        return success(spuModelService.getList(spuModelBo));
    }

    /**
     * 新增数据
     *
     * @param spuModelDto 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    public Result<Boolean> insert(@RequestBody SpuModelDto spuModelDto) {
        boolean result = spuModelService.addSpuModel(spuModelDto);
        return result ? Result.success("添加成功") : Result.error("添加失败");
    }

    /**
     * 修改数据
     *
     * @param spuModelDto 实体对象
     * @return 修改结果
     */
    @PostMapping("upd")
    public Result<Boolean> update(@RequestBody SpuModelDto spuModelDto) {
         boolean result = spuModelService.updateSpuModel(spuModelDto);
        return result ? Result.success("修改成功") : Result.error("修改失败");
    }

    /**
     * 删除数据
     *
     * @return 删除结果
     */
    @PostMapping("del")
    public Result<Boolean> delete(@RequestBody SpuModelDto spuModelDto) {
        boolean result = spuModelService.removeById(spuModelDto.getId());
        return result ? Result.success("删除成功") : Result.error("删除失败");
    }

}
